import 'package:basic_common/app_theme.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_patient/model/patient_data_list.dart';
import 'package:basic_widgets/image_widget.dart';
import 'package:basic_widgets/input_widget.dart';
import 'package:basic_widgets/text_widget.dart';


import 'patient_search_controller.dart';
class PatientSearchPage extends GetView<PatientSearchController> {
  const PatientSearchPage({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: searchBox(
          searchController: controller.searchController,
          isEnable: true,
          onSubmitted: (value) => {controller.searchPatientList()},
        ),
        elevation: 0,
        automaticallyImplyLeading: false,
      ),
      body: Padding(
        padding: EdgeInsets.symmetric(
            horizontal: ScreenHelper.width(15),
            vertical: ScreenHelper.height(10)),
        child: GetBuilder<PatientSearchController>(
          id: 'searchList',
          builder: (controller) => controller.patientList!.length == 0 ||
                  controller.patientList!.isEmpty
              ? Center(
                  child: EmptyBox(title: "暂无患者"),
                )
              : Column(
                  children: [
                    controller.patientList!.length == 0
                        ? SizedBox.shrink()
                        : Container(
                            alignment: Alignment.centerLeft,
                            padding: EdgeInsets.symmetric(
                                vertical: ScreenHelper.width(10)),
                            child: Text(
                              "为您搜索到以下患者",
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                  color: AppTheme.dartWeakTextColor,
                                  fontSize: ScreenHelper.sp(15)),
                            )),
                    Expanded(
                      child: Container(
                          height: ScreenHelper.screenHeight,
                          child: ListView.separated(
                              itemBuilder: (context, index) {
                                PatientData itemdata =
                                    controller.patientList![index];
                                return buildListItem(itemData: itemdata);
                              },
                              separatorBuilder: (context, index) => divider(),
                              itemCount: controller.patientList!.length)),
                    ),
                  ],
                ),
        ),
      ),
    );
  }

  Widget buildListItem({required dynamic itemData}) {
    return GestureDetector(
      onTap: () {
        Get.toNamed(AppRoutes.patientDetail,
            arguments: {"consumerNo": itemData.consumerNo});
      },
      child: Container(
        padding: EdgeInsets.symmetric(
            vertical: ScreenHelper.height(10),
            horizontal: ScreenHelper.height(10)),
        height: ScreenHelper.height(ScreenHelper.height(100)),
        color: Colors.white,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            circleImg(
                width: ScreenHelper.width(52),
                head: itemData.patitentAvatarUrl ?? ''),
            SizedBox(
              width: ScreenHelper.width(10),
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      itemData.nickName ?? '',
                      style: TextStyle(fontSize: ScreenHelper.sp(18)),
                    ),
                    SizedBox(
                      width: ScreenHelper.width(10),
                    ),
                    Text(
                        "${itemData.age != null ? itemData.age.toInt().toString() : 0}岁"),
                    SizedBox(
                      width: ScreenHelper.width(10),
                    ),
                    Image.asset(
                      itemData.gender == '1'
                          ? "assets/profile/icon_sex_male.png"
                          : 'assets/profile/icon_female.png',
                      width: ScreenHelper.width(14),
                      height: ScreenHelper.width(14),
                      package: "basic_common",
                    ),
                  ],
                ),
                SizedBox(
                  height: ScreenHelper.height(15),
                ),
                Container(
                  width: ScreenHelper.width(200),
                  height: ScreenHelper.height(24),
                  // child: ListView(
                  //   scrollDirection: Axis.horizontal,
                  //   children:
                  //       buildIdenity(tagStr: itemData.tagsFromDoctor ?? ''),
                  // ),
                  child: Text(itemData.labelName??"",style: TextStyle(color: AppTheme.dartWeakTextColor,fontSize: ScreenHelper.sp(14)),),
                )
              ],
            ),
            Expanded(
              child: Container(
                width: ScreenHelper.width(90),
                height: ScreenHelper.height(30),
                alignment: Alignment.center,
                padding: EdgeInsets.symmetric(vertical: 4),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(50),
                  color: Color.fromRGBO(255, 241, 240, 1),
                ),
                
                child: Text(
                  controller.signStatusTostr(itemData.signStatus??""),
                  style: TextStyle(color: AppTheme.redtextColor),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  /// 医生身份标签项
  List<Widget> buildIdenity({required String tagStr}) {
    List<Widget> tagWidgetList = [];
    if (tagStr.contains(',')) {
      List<String> tagList = tagStr.split(',');
      tagList.forEach((element) {
        tagWidgetList.add(tagItem(element));
      });
    }
    return tagWidgetList;
  }

  Widget tagItem(String tag) {
    return Container(
      alignment: Alignment.center,
      width: ScreenHelper.width(50),
      height: ScreenHelper.height(24),
      margin: EdgeInsets.symmetric(horizontal: ScreenHelper.width(4)),
      decoration: BoxDecoration(
          border: Border.all(color: AppTheme.primyColor),
          borderRadius: BorderRadius.circular(6)),
      child: Text(
        tag,
        style: TextStyle(
            color: AppTheme.primyColor, fontSize: ScreenHelper.sp(12)),
      ),
    );
  }
}
